import request from '@/utils/request'
import { praseStrEmpty } from '@/utils/common'

// 查询验收任务列表
export function listQaTask(query) {
  return request({
    url: '/mark/qaTaskV1/list',
    method: 'get',
    params: query,
  })
}

// 查询验收任务列表（排除节点）
export function listQaTaskExcludeChild(taskId) {
  return request({
    url: '/mark/qaTaskV1/list/exclude/' + taskId,
    method: 'get',
  })
}

// 查询验收任务详细
export function getQaTask(taskId) {
  return request({
    url: '/mark/qaTaskV1/' + praseStrEmpty(taskId),
    method: 'get',
  })
}

// 查询验收任务下拉树结构
export function treeselect() {
  return request({
    url: '/mark/qaTaskV1/treeselect',
    method: 'get',
  })
}

// 标注结果汇总，传入父任务id：qaTaskId，自动连接trackid
export function summarize(taskId) {
  return request({
    url: '/mark/qaTaskV1/summarize/' + taskId,
    method: 'put',
  })
}

// 标注结果批量汇总，传入父任务ids：taskIds，自动连接trackid
export function batchSummarize(data) {
  return request({
    url: '/mark/qaTaskV1/batchSummarize',
    method: 'post',
    data: data,
  })
}

// 根据角色ID查询验收任务树结构
export function roleQaTaskTreeselect(roleId) {
  return request({
    url: '/mark/qaTaskV1/roleQaTaskTreeselect/' + roleId,
    method: 'get',
  })
}

// 新增验收任务
export function addQaTask(data) {
  return request({
    url: '/mark/qaTaskV1',
    method: 'post',
    data: data,
  })
}

// 修改验收任务
export function updateQaTask(data) {
  return request({
    url: '/mark/qaTaskV1',
    method: 'put',
    data: data,
  })
}

export function batchUpdate(data) {
  return request({
    url: '/mark/qaTaskV1/batchUpdate',
    method: 'put',
    data: data,
  })
}

// 通过验收任务
export function updateQaTaskStatus(data) {
  return request({
    url: '/mark/qaTaskV1/updateQaTaskStatus',
    method: 'post',
    data: data,
  })
}

// 删除验收任务
export function delQaTask(taskId) {
  return request({
    url: '/mark/qaTaskV1/' + taskId,
    method: 'delete',
  })
}

// 执行自动质检
export function taskAutoCheck(params) {
  return request({
    url: '/mark/qaTaskV1/taskAutoCheck',
    method: 'post',
    params: params,
  })
}

// 任务文件扫描
export function taskInitialize(params) {
  return request({
    url: '/mark/qaTaskV1/taskInitialize',
    method: 'post',
    data: params,
  })
}

// 获取子任务即其所有的父任务
export async function getTaskWithParent(taskId, dep = ' -> ') {
  const { data: task } = await getQaTask(taskId)
  let tmp = task
  const taskPaths = [tmp.taskName]
  for (let count = 1; tmp.parentId !== '0' && count < 3; count++) {
    const { data } = await getQaTask(tmp.parentId)
    task.parent = data
    tmp = data
    taskPaths.push(tmp.taskName)
  }
  task.path = taskPaths.reverse().join(dep)
  return task
}
